package com.link2room.ironhide.facade.impl;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.BaseL2rParamDto;
import com.link2room.common.jpa.condition.ConditionDetail;
import com.link2room.common.jpa.condition.L2RCondition;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.PinyinUtil;
import com.link2room.ironhide.dto.param.manager.L2RAreaManagerParamDto;
import com.link2room.ironhide.dto.result.base.L2RCityDto;
import com.link2room.ironhide.dto.result.base.L2RCountryDto;
import com.link2room.ironhide.dto.result.base.L2RDistrictDto;
import com.link2room.ironhide.dto.result.base.L2RProvinceDto;
import com.link2room.ironhide.entity.*;
import com.link2room.ironhide.facade.IAreaManagerFacadeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by sss on 2016/10/11.
 */
@Component("areaManagerFacadeService")
public class AreaManagerFacadeServiceImpl implements IAreaManagerFacadeService {
    @Resource
    private ICommonService commonService;

    /**
     * 国家列表
     * @throws Exception
     * @param baseL2rParamDto
     */
    @Override
    public QueryResult<L2RCountryDto> listL2RCountry(BaseL2rParamDto baseL2rParamDto)throws Exception{
        QueryResult<L2RCountry> l2rDCountryResult = commonService.findAllEQ(L2RCountry.class, null);
        return ClassConverUtil.copyQueryResultToDtoQueryResult(l2rDCountryResult, L2RCountryDto.class, false);
    }

    /**
     * 省份列表
     * @throws Exception
     * @param l2RAreaManagerParamDto
     */
    @Override
    public QueryResult<L2RProvinceDto> listL2RProvice(L2RAreaManagerParamDto l2RAreaManagerParamDto)throws Exception{
        L2RProvince queryLP=new L2RProvince();
        queryLP.setCountryCode(l2RAreaManagerParamDto.getCountryCode());
        QueryResult<L2RProvince> l2rProvinceResult = commonService.findAllEQ(L2RProvince.class, queryLP);
        return ClassConverUtil.copyQueryResultToDtoQueryResult(l2rProvinceResult, L2RProvinceDto.class, false);
    }

    /**
     * 城市列表
     * @throws Exception
     * @param l2RAreaManagerParamDto
     */
    @Override
    public QueryResult<L2RCityDto> listL2RCity(L2RAreaManagerParamDto l2RAreaManagerParamDto)throws Exception{
        L2RCity queryLC=new L2RCity();
        queryLC.setProvinceCode(l2RAreaManagerParamDto.getProvinceCode());
        QueryResult<L2RCity> l2rCityResult = commonService.findAllEQ(L2RCity.class, queryLC);
        return ClassConverUtil.copyQueryResultToDtoQueryResult(l2rCityResult, L2RCityDto.class, false);
    }

    /**
     * 区域列表
     * @throws Exception
     * @param l2RAreaManagerParamDto
     */
    @Override
    public QueryResult<L2RDistrictDto> listL2RDistrict(L2RAreaManagerParamDto l2RAreaManagerParamDto)throws Exception{
        L2RDistrict queryLD=new L2RDistrict();
        queryLD.setCityCode(l2RAreaManagerParamDto.getCityCode());
        QueryResult<L2RDistrict> l2rDistrictResult = commonService.findAllEQ(L2RDistrict.class, queryLD);
        return ClassConverUtil.copyQueryResultToDtoQueryResult(l2rDistrictResult, L2RDistrictDto.class, false);
    }

    /**
     * 根据区域代码查询
     * @throws Exception
     * @param l2RAreaManagerParamDto
     */
    @Override
    public L2RDistrictDto getL2RDistrict(L2RAreaManagerParamDto l2RAreaManagerParamDto)throws Exception{
        L2RDistrict queryLD=new L2RDistrict();
        queryLD.setDistrictCode(l2RAreaManagerParamDto.getDistrictCode());
        L2RDistrict l2rDistrict = commonService.findOneEQ(L2RDistrict.class, queryLD);
        return ClassConverUtil.copyPropertiesToDto(l2rDistrict, L2RDistrictDto.class, false);
    }

    @Override
    public void importL2RPositionBase(L2RAreaManagerParamDto l2RAreaManagerParamDto)throws Exception{
        ImportCc queryCc=new ImportCc();
        queryCc.setCol0(l2RAreaManagerParamDto.getCityCode().toString());
        QueryResult<ImportCc> rootResult = commonService.findAllEQ(ImportCc.class, queryCc, null, null, null, new String[]{ImportCc.ImportCc_.col1.toString()});
        List<ImportCc> rootList = rootResult.getResultlist();
        List<String> rootNames=null;
        List<L2RPositionBase> l2RPositionBaseList=new ArrayList<>();
        if(rootList!=null&&rootList.size()>0){
            rootNames=new ArrayList<>();
            for(ImportCc root:rootList){
                L2RPositionBase l2rPositionBase=new L2RPositionBase();
                l2rPositionBase.setLhotelGroupCode(DefaultValue.LHOTEL_GROUP_CODE);
                l2rPositionBase.setLhotelCode(DefaultValue.LHOTEL_CODE);
                l2rPositionBase.setCityCode(Long.valueOf(root.getCol0()));
                l2rPositionBase.setName(root.getCol1());
                l2rPositionBase.setPositionType("ROOT");
                l2rPositionBase.setPositionCode(PinyinUtil.getStringPinYin(root.getCol1()));
                l2RPositionBaseList.add(l2rPositionBase);
                rootNames.add(root.getCol1());
            }
        }
        L2RCondition l2rConfition=new L2RCondition();
        l2rConfition.setAndConditions(new ConditionDetail[]{
                new ConditionDetail(ImportCc.ImportCc_.col0.toString(), CriteriaUtil.Operator.EQ,l2RAreaManagerParamDto.getCityCode().toString()),
                new ConditionDetail(ImportCc.ImportCc_.col2.toString(), CriteriaUtil.Operator.NOTIN,rootNames)
        });
        QueryResult<ImportCc> firstResult = commonService.findAllByCondition(ImportCc.class, l2rConfition, null, null, null, new String[]{ImportCc.ImportCc_.col2.toString()});
        List<ImportCc> firstList = firstResult.getResultlist();
        if(firstList!=null&&firstList.size()>0){
            for(ImportCc first:firstList){
                L2RPositionBase l2rPositionBase=new L2RPositionBase();
                l2rPositionBase.setLhotelGroupCode(DefaultValue.LHOTEL_GROUP_CODE);
                l2rPositionBase.setLhotelCode(DefaultValue.LHOTEL_CODE);
                l2rPositionBase.setCityCode(Long.valueOf(first.getCol0()));
                l2rPositionBase.setName(first.getCol2());
                l2rPositionBase.setPositionType(PinyinUtil.getStringPinYin(first.getCol1()));
                l2rPositionBase.setPositionCode(PinyinUtil.getStringPinYin(first.getCol2()));
                l2RPositionBaseList.add(l2rPositionBase);
            }
        }

        QueryResult<ImportCc> endResult = commonService.findAllEQ(ImportCc.class, queryCc);
        List<ImportCc> endList = endResult.getResultlist();
        if(endList!=null&&endList.size()>0){
            for(ImportCc end:endList){
                L2RPositionBase l2rPositionBase=new L2RPositionBase();
                l2rPositionBase.setLhotelGroupCode(DefaultValue.LHOTEL_GROUP_CODE);
                l2rPositionBase.setLhotelCode(DefaultValue.LHOTEL_CODE);
                l2rPositionBase.setCityCode(Long.valueOf(end.getCol0()));
                l2rPositionBase.setName(end.getCol3());
                l2rPositionBase.setPositionType(PinyinUtil.getStringPinYin(end.getCol2()));
                l2rPositionBase.setPositionCode(PinyinUtil.getStringPinYin(end.getCol3()));
                l2RPositionBaseList.add(l2rPositionBase);
            }
        }
        L2RPositionBase querypb=new L2RPositionBase();
        querypb.setCityCode(l2RAreaManagerParamDto.getCityCode());
        QueryResult<L2RPositionBase> l2RPositionBaseResult = commonService.findAllEQ(L2RPositionBase.class, querypb);
        List<L2RPositionBase> l2RPbList = l2RPositionBaseResult.getResultlist();
        Iterator<L2RPositionBase> iterator = l2RPositionBaseList.iterator();
        while (iterator.hasNext()){
            L2RPositionBase l2rPositionBase=iterator.next();
            if(l2RPbList!=null){
                for(L2RPositionBase l2rPb:l2RPbList){
                    if(l2rPositionBase.getPositionType().equals(l2rPb.getPositionType())&&l2rPositionBase.getPositionCode().equals(l2rPb.getPositionCode())) {
                        iterator.remove();
                    }
                }
            }
        }
        commonService.saveList(l2RPositionBaseList);
    }
}
